package com.qidian.mes.cal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.cal.mapper.CalTeamMemberMapper;
import com.qidian.mes.cal.service.CalTeamMemberService;
import com.qidian.mes.pojo.cal.CalPlanTeam;
import com.qidian.mes.pojo.cal.CalTeam;
import com.qidian.mes.pojo.cal.CalTeamMember;
import com.qidian.mes.pojo.cal.CalUser;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 04冯虹丹
 * @Company 起点编程
 * @Time 2023/10/30 16:21
 * @Description
 */
@Service
@Transactional
public class CalTeamMemberServiceImpl implements CalTeamMemberService {

    @Autowired
    private CalTeamMemberMapper calTeamMemberMapper;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param calTeamMember
     * @return
     */
    @Override
    public MESResult list(int page, int limit, CalTeamMember calTeamMember) {
        PageHelper.startPage(page,limit);
        List<CalTeamMember> list = calTeamMemberMapper.list(calTeamMember);
        PageInfo<CalTeamMember> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }


    /**
     * 根据班组成员ID单个删除
     * @param memberId
     * @return
     */
    @Override
    public MESResult delete(Long memberId) {
        return MESResult.updateIsOk(calTeamMemberMapper.delete(memberId));
    }

    /**
     * 根据班组成员ID批量删除
     * @param memberId
     * @return
     */
    @Override
    public MESResult deletes(Long[] memberId) {
        int count = calTeamMemberMapper.deletes(memberId);
        return MESResult.ok(count + "");
    }

    /**
     * 添加班组成员
     * @param userIds
     * @param teamId
     * @return
     */
    @Override
    public MESResult save(Long[] userIds, Long teamId) {
        for (int i = 0; i < userIds.length-1; i++) {
            if(calTeamMemberMapper.selectCalTeamMember(userIds[i]) != null){
                return MESResult.duplicate("已分配过班组,不可重复分配!");
            }else{
                CalUser calUser = calTeamMemberMapper.selectCalUser(userIds[i]);
                CalTeamMember calTeamMember = new CalTeamMember();
                calTeamMember.setTeamId(teamId);
                calTeamMember.setUserId(calUser.getUserId());
                calTeamMember.setUserName(calUser.getUserName());
                calTeamMember.setNickName(calUser.getNickName());
                calTeamMember.setTel(calUser.getTel());
                calTeamMemberMapper.save(calTeamMember);
            }
        }
        return MESResult.ok();
    }


}
